package com.atguigu.common.valid;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class ListValueConstraintValidator implements ConstraintValidator<ListValue,Integer> {

      private Set<Integer> set=new HashSet<>();
    @Override
    public void initialize(ListValue constraintAnnotation) {
        //获取注解添加在值
        int[] vals = constraintAnnotation.vals();
        Arrays.stream(vals).forEach((item)->{
            set.add(item);
        });
    }

    @Override
    public boolean isValid(Integer integer, ConstraintValidatorContext constraintValidatorContext) {
        //判断set中是否包含integer,若有返回true
        return set.contains(integer);
    }
}
